package com.huqu.beans.hoidap;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import com.huqu.beans.sessionWeb.SessionWeb;
import com.huqu.dao.N300Dao;
import com.huqu.entities.N100;
import com.huqu.entities.N200;
import com.huqu.entities.N300;
import com.huqu.entities.Page;
import com.huqu.utils.ApplicationConstant;
import com.huqu.utils.ApplicationHelper;

@ManagedBean(name="hoidapBean")
@ViewScoped
public class HoiDapBean implements Serializable{
	@ManagedProperty("#{sessionWeb}")
	private SessionWeb sessionWeb;
	private String vLang="VN";
	private Page page=new Page();
	
	private String loaicauhoi="";
	private int idloaicauhoi=0;
	// la nganh-nghe= true; tuyen-sinh=false
	private boolean renderTitle=false;
	
	private List<N300> listN300= new ArrayList<N300>();
	
	// ===================================================================================================================
	// =====================================================FUNCTIONS=====================================================
		@PostConstruct
		public void init(){
			try {
				System.out.println("====== HoiDapBean =======");
				page.setLimit(ApplicationConstant.LIMIT);
				
				if(ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LANG)!=null){
					vLang=ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LANG).toString().toUpperCase();
					
					if(vLang.trim().toUpperCase().equals("VN")){
						if(sessionWeb.locale.equals(Locale.US)){
							sessionWeb.setLocale(new Locale("vi", "VN"));
							FacesContext.getCurrentInstance()
			    			.getViewRoot().setLocale(new Locale("vi", "VN"));
							RequestContext.getCurrentInstance().execute( "location.reload();");
						}
					}else if(vLang.trim().toUpperCase().equals("EN")){
						if(sessionWeb.locale.equals(new Locale("vi", "VN"))){
							sessionWeb.setLocale(Locale.US);
							FacesContext.getCurrentInstance()
			    			.getViewRoot().setLocale(Locale.US);
							RequestContext.getCurrentInstance().execute( "location.reload();");
						}
					}
				}else{
					System.out.println("---language khong xac dinh ");
				}
				
				if(ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LOAICAUHOI)!=null){
					loaicauhoi=ApplicationHelper.getRequest().getParameter(ApplicationConstant.PRETTY_PARA_LOAICAUHOI).trim().toLowerCase();
					if(loaicauhoi.equals("nganh-nghe")){
						idloaicauhoi=9;
						renderTitle=true;
					}else if(loaicauhoi.equals("tuyen-sinh")){
						idloaicauhoi=10;
						renderTitle=false;
					}
				}
				
				getListOfTabN300();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		// load danh sách các câu hỏi
		public void getListOfTabN300(){
			try {
				N300Dao n300Dao = (N300Dao) ApplicationHelper.findBean(ApplicationConstant.REPOSITORY_NAME_N300);
				System.out.println("---ListOfTabN300( "+0+","+ idloaicauhoi+","+ page.getOffset()+","+ page.getLimit()+","+ 1+","+ 1+","+ 1+","+ vLang+" )");
				listN300=n300Dao.ListOfTabN300(0, idloaicauhoi, page.getOffset(), page.getLimit(), 1, 1, 1, vLang);
				if(listN300.size()>0){
					page.setTotalpage(listN300.get(0).getTotalrow(),page.getLimit());
				}else{page.setTotalpage(0);}
				System.out.println("ListN100.size()= "+listN300.size());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		// phân trang
		public void goToPage(){
			try {
				System.out.println("--- GoToPage( )");
				Map<String, String> params = FacesContext.getCurrentInstance()
						.getExternalContext().getRequestParameterMap();
				int numberPage = Integer.parseInt(params.get("numberpage"));
				page.setOffset((numberPage-1)*page.getLimit());
				page.setCurrentpage(numberPage);
				System.out.println("--- numberpage= "+numberPage);
				getListOfTabN300();
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		public SessionWeb getSessionWeb() {
			return sessionWeb;
		}

		public void setSessionWeb(SessionWeb sessionWeb) {
			this.sessionWeb = sessionWeb;
		}

		public String getvLang() {
			return vLang;
		}

		public void setvLang(String vLang) {
			this.vLang = vLang;
		}

		public Page getPage() {
			return page;
		}

		public void setPage(Page page) {
			this.page = page;
		}

		public List<N300> getListN300() {
			return listN300;
		}

		public void setListN300(List<N300> listN300) {
			this.listN300 = listN300;
		}

		
		public boolean isRenderTitle() {
			return renderTitle;
		}

		public void setRenderTitle(boolean renderTitle) {
			this.renderTitle = renderTitle;
		}
		
		
		

}
